// @ts-ignore
/* eslint-disable */
import {request} from 'umi';
import {DocumentListItem, DocumentBody} from './data';
import {Key} from 'react';

export async function dataList(
  params: {
    // query
    /** 当前的页码 */
    current?: number;
    /** 页面的容量 */
    pageSize?: number;
    category?: number;
  },
  sorter: any,
) {
  return request<{
    data: DocumentListItem[];
    /** 列表的内容总数 */
    total?: number;
    success?: boolean;
  }>('/v2/enterprise/document/list', {
    method: 'GET',
    params: {
      ...params,
      pageNum: params.current,
      orderByColumn: Object.keys(sorter)[0],
      isAsc: Object.values(sorter)[0],
    },
  });
}

export async function loadData(param: DocumentBody): Promise<{ data: DocumentBody }> {
  return request(`/v2/enterprise/document/${param.documentId}`, {method: 'GET'});
}

export async function addData(param: DocumentBody): Promise<{ data: number }> {
  return request('/v2/enterprise/document', {method: 'POST', data: param});
}

export async function updateData(param: DocumentBody): Promise<{ data: number }> {
  return request('/v2/enterprise/document', {method: 'PUT', data: param});
}


export async function removeData(data: number[], options?: { [key: string]: any }) {
  return request<Record<string, any>>(`/v2/enterprise/document/` + data.join(), {
    data,
    method: 'DELETE',
    ...(options || {}),
  });
}

export async function loadSelectTag() {
  return request(`/v2/enterprise/document/tags`, {method: 'GET'});
}

export async function download() {
  request(`/v2/enterprise/document/question/template`, {
    method: 'POST',
    // 必须加responseType: 'blob',
    responseType: 'blob',
  }).then((res) => {
    const objectURL = URL.createObjectURL(res);
    let btn = document.createElement('a');
    btn.download = '试题模板.xlsx'; //文件类型
    btn.href = objectURL;
    btn.click();
    URL.revokeObjectURL(objectURL);
    // @ts-ignore
    btn = null;
  });
}

export async function coursewareListSave(dataId: number, ids: Key[]) {
  return request(`/v2/enterprise/document/courseware/save/${dataId}`, {
    data: ids,
    method: 'POST',
  });
}

export async function coursewareList(dataId: number) {
  return request(`/v2/enterprise/document/courseware/${dataId}`, {
    method: 'GET',
  });
}

export async function employeeListSave(dataId: number, ids: Key[]) {
  return request(`/v2/enterprise/document/employee/save/${dataId}`, {
    data: ids,
    method: 'POST',
  });
}

export async function employeeList(dataId: number) {
  return request(`/v2/enterprise/document/employee/${dataId}`, {
    method: 'GET',
  });
}
